import request from '@/utils/request'
import { getDepartmentSelector } from '@/api/permission/department'
export default {
  components: {},
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      information: false,
      dataForm: {
        id: '',
        courseName: '',
        courseObjective: '',
        courseIntroduction: '',
        targetAudience: '',
        courseDuration: '',
        teacherIds: '',
        courseOutline: [
          {
            name: '',
            content: ''
          }, {
            name: '',
            content: ''
          }
        ],
        materials: []
      },
      departmentTreeData: [],
      genderOptions: [],
      targetAudienceOptions: [
        {
          enCode: '新手教师',
          fullName: '新手教师'
        },
        {
          enCode: '有一定经验教师',
          fullName: '有一定经验教师'
        }
      ],
      rules: {
        courseName: [
          {
            required: true,
            message: '请输入课程名称',
            trigger: 'blur'
          }
        ],
        courseObjective: [
          {
            required: true,
            message: '请输入课程目标',
            trigger: 'blur'
          }
        ],
        courseIntroduction: [
          {
            required: true,
            message: '请输入课程简介',
            trigger: 'change'
          }
        ],
        targetAudience: [
          {
            required: true,
            message: '请选择适用对象',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(row, information) {
      this.dataForm = {
        id: '',
        courseName: '',
        courseObjective: '',
        courseIntroduction: '',
        targetAudience: '',
        courseDuration: '',
        teacherIds: '',
        courseOutline: [
          {
            name: '',
            content: ''
          }, {
            name: '',
            content: ''
          }
        ],
        materials: []
      }
      this.dataForm.id = row ? row.id : 0
      this.information = information
      this.visible = true
      this.$nextTick(() => {
        this.$refs['elForm'].clearValidate();
        if (this.dataForm.id) {

          request({
            url: '/api/train/course/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            if (!res.data.courseOutline || res.data.courseOutline.length < 1) {
              res.data.courseOutline =  [
                {
                  name: '',
                  content: ''
                }, {
                  name: '',
                  content: ''
                }
              ]
            }
            if (!res.data.materials ) {
              res.data.materials =  undefined
            }
            this.dataForm =  res.data
          })
        }
      })
    },
    onDeleteLine (index) {
      this.dataForm.courseOutline.splice(index, 1)
    },
    onAddLine () {
      this.dataForm.courseOutline.push({
        name: '',
        content: ''
      })
    },
    dataFormSubmit() {
      this.searchTimer && clearTimeout(this.searchTimer)
      this.searchTimer = setTimeout(() => {
        this.$refs['elForm'].validate(valid => {
          if (valid) {
            if (!this.dataForm.id) {
              request({
                url: `/api/train/course`,
                method: 'post',
                data: this.dataForm
              }).then(res => {
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refresh', true)
                  }
                })
              })
            } else {
              request({
                url: '/api/train/course/' + this.dataForm.id,
                method: 'PUT',
                data: this.dataForm
              }).then(res => {
                this.$message({
                  message: res.msg,
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refresh', true)
                  }
                })
              })
            }
          }
        })
      })
    }
  }
}
